/**
 * 
 */
package edu.uwm.nlp.jude.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.uwm.nlp.jude.internal.IErrorCode;

/**
 * @author qing
 *
 * Jun 15, 2009
 */
public class StringUtil {
	
	/**
	 * find next char which is not space,\t, or \n
	 * @param start index to scan
	 * @return the valid char
	 *  1024 if no valid char in this string from start
	 */
	public static char nextNonEmptyChar(String str, int start)
	{
		int idx = start;
		while(idx < str.length())
		{
			char point = str.charAt(idx);
			if(point ==' ' || point =='\t'||point=='\n')
				idx++;
			else
				return point; // find a valid char
		}
		
		return IErrorCode.NO_VALID_CHAR_RETURN;
	}
	
	public static boolean isAllNames(String str)
	{
		return str.matches("[A-Za-z\\,\\.\\r\\(\\s\\n\\-]*");
							// year might be in a ( )
	}
	
	/**
	 * replace 8211'-' with normal '-'
	 * @param str
	 * @return the string which contain normal '-'
	 */
	public static String replaceHyphen8211(String str){
		
		String result;
		char[] chs = new char[str.length()];
		for(int i=0 ; i<str.length(); i++)
		{
			chs[i] = str.charAt(i);
			if(chs[i]==8211)
				chs[i] = '-';
		}
		result = String.valueOf(chs);
		return result;
	}
	
	public static String removeEscp(String str)
	{
		String res = new String();
		char[] chs = str.toCharArray();
		for(int i=0; i< chs.length; i++)
		{
			if(!(chs[i]=='\n'||chs[i]=='\r'))
				res = res.concat(String.valueOf(chs[i]));
		}
		return res;
	}
	
	public static boolean hasSpace(String str)
	{
		return str.trim().indexOf(' ')!=-1;
	}

	public static String getContext(String text, int front,int back, int words)
	{
		int index = front;
		int i =0;
		int start;
		int end;
		while(i<words && index >=0)
		{
			if(text.charAt(index)==' ')
				i++;
			index--;
		}
		
		index++;
		
		if(index <0)
			index = 0;
		start = index;
		
		int fullTextLength = text.length();
		i = 0;
		index = back;
		while(i< words && index <fullTextLength)
		{
			if(text.charAt(index)==' '||text.charAt(index)=='\n')
				i++;
			index++;
		}
		if(index ==fullTextLength)
			index = fullTextLength-1;
		end = index;
		
		return text.substring(start, end);
		
	}
	
	public static int skipEscps(String str, int index)
	{
		int result = index;
		int size = str.length();
		while(result <size)
		{
			if(str.charAt(result)=='\n'||str.charAt(result)=='\r'||str.charAt(result)=='\t'||str.charAt(result)==' ')
				result++;
			else
				break;
		}
		if(result<size)
			return result-1;
		else
			return size;
	}
	/**
	 * check if the char at index is a half pranphais
	 * @param text
	 * @param index position of ')'
	 * @return
	 */
	public static boolean isInHalfPranph(String text, int index)
	{
		if(index < 2) return true;
		int prevLeft = text.lastIndexOf('(', index-1);
		int prevRight = text.lastIndexOf(')', index-1);
		if(prevLeft == -1) 
			return true;
		else if(prevRight == -1)
			return false;
		else if(prevLeft < prevRight)
			return true;
		else
			return false;
	}
	
	public static String getCurrentLine(String text, int index)
	{
		String str = null;
		int end = text.indexOf('\n', index+1);
		int start = text.lastIndexOf('\n', index);
		if(start == -1)
			start = 0;
		else if(end == -1)
			str = text.substring(start);
			else
				str = text.substring(start, end);  
		return str;
		
	}
	
	public static String getPreviousWord(String text, int index)
	{
		int end = -1;
		int start = -1;
		int i;
		for(i=index-1; i>0; i--)
		{
			char ch = text.charAt(i);
			if(ch == ' '|| ch=='\n' || ch=='\t'|| ch ==',' || ch == '.')
			{
				end = i;
				break;
			}
		}
		if(i==0)
			return null;
		
		for(i = end-1; i>0; i--)
		{
			char ch = text.charAt(i);
			if(ch == ' '|| ch=='\n' || ch=='\t')
			{
				start = i;
				break;
			}
		}
		return text.substring(start+1, end);
	}
	
	public static boolean isInFormula(String text, int index, int threshold)
	{
		String prevWord = getPreviousWord(text, index);
		if(prevWord == null) return false;
		Pattern p1 = Pattern.compile("[0-9]+");
		Matcher m1 = p1.matcher(prevWord);
		
		return m1.find() || (prevWord.length()<= threshold);
	}

	public  static boolean isInNumberPool(String text, int start, int end, int threshold)
	{
		String context = getContext(text, start, end, 5);
		String[] words = context.split("[\\n\\s,.]");
		int sum = 0;
		for(int i=0; i<words.length; i++)
			sum+= words[i].length();
		int avg = sum / words.length;
		return (avg<threshold);
	}

}
